package com.crazyant;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MapasView extends SurfaceView  {
	private boolean surfacecreated;
	private boolean threadrunning;
	private MapasDrawThread thread;
	private SurfaceHolder holder;
	private Paint fons;
	private float pos_act, pos_mou, pos_inicial;
	public int mou_enrere;
	public int mou;
	
	private Portada portada;
	public MapasView(Context context) {
		super(context);
		init();
	}
	
	public MapasView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MapasView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	private void init() {
		surfacecreated = false;
		threadrunning = false;
		holder = getHolder();
		holder.addCallback(new Callback() {
			
			public void surfaceDestroyed(SurfaceHolder holder) {
				stopthread();
				surfacecreated = false;
			}
			
			public void surfaceCreated(SurfaceHolder holder) {
				holder.setFormat(PixelFormat.RGBA_8888);
				fons = new Paint();
				//Shader shader = new LinearGradient(GV.Screen.metrics.widthPixels,0,GV.Screen.metrics.widthPixels,GV.Screen.metrics.heightPixels/1.2f,new int[]{0xFF000033,0xFF000099},null,Shader.TileMode.CLAMP);
				fons.setColor(0xFF00CCFF);
				//fons.setShader(shader);
				surfacecreated = true;
				mou_enrere = 0;
				mou = 0;
				GV.Control_mapas.parar = 0;
				portada = new Portada();
			}
			
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {
					startthread();
			}
		});
			
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawRect(0, 0, getWidth(),getHeight(), fons);
		if(mou_enrere == 1) {
			portada.act_torna(pos_act-pos_inicial);
		}
		if(mou == 1) {
			portada.act();
		}
		for(int i = 0; i < GV.Instancies.fondos.size(); ++i) {
			GV.Instancies.fondos.get(i).draw(canvas);
		}
	}
	
	public void startthread() {
		if(!surfacecreated) return;
		if(threadrunning) return;
		thread = new MapasDrawThread(this);
		thread.setRunning(true);
		thread.start();
		threadrunning = true;
	}
	public void stopthread() {
		if(!surfacecreated) return;
		if(!threadrunning) return;
		boolean retry = true;
		while (retry) {
			try {
				thread.setRunning(false);
				thread.join();
				threadrunning = false;
				retry = false;
				
			} catch (InterruptedException e) {}
		}
	}

	public boolean onTouchEvent(MotionEvent e) {
		// detectamos movimiento
		if(e.getAction() == e.ACTION_DOWN) {
			pos_act = e.getX();
			pos_inicial = e.getX();
		}
		if(e.getAction() == e.ACTION_MOVE) {
			pos_mou = e.getX()-pos_act;
			portada.mov(pos_mou);
			pos_act = e.getX();
		}
		if(e.getAction() == e.ACTION_UP) {
			if(pos_act == pos_inicial) {
				int mundo = GV.Instancies.fondos.get(1).mundo;
				GV.Instancies.mapas.handler.sendEmptyMessage(mundo);
			}
			if (Math.abs(pos_act-pos_inicial) < GV.widthpercent(40)) {
				portada.torna(pos_act-pos_inicial);
				mou_enrere = 1;
			}
			else {
				portada.movex(pos_mou*1.5f);
				mou = 1;
			}
		}
		return true;
	}
}
